package com.wen.edu.controller;


import com.wen.commonutils.ResultEntity;
import com.wen.edu.api.VodApi;
import com.wen.edu.entity.EduCourse;
import com.wen.edu.entity.vo.CourseInfoVO;
import com.wen.edu.entity.vo.CoursePublishVo;
import com.wen.edu.service.EduCourseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wen
 * @since 2021-05-29
 */
@RestController
@RequestMapping("/edu/service/course")
public class EduCourseController {
    @Resource
    private EduCourseService eduCourseService;
    @Resource
    private VodApi vodApi;

    /**
     * 查询所有的课程信息
     */
    @GetMapping
    public ResultEntity getCourseList() {
        List<EduCourse> list = eduCourseService.list(null);
        return ResultEntity.ok().data("list", list);
    }

    /**
     * 添加课程基本信息
     *
     * @param courseInfoVO 课程基本信息
     * @return result of operation
     */
    @PostMapping("addCourseInfo")
    public ResultEntity addCourseInfo(@RequestBody CourseInfoVO courseInfoVO) {
//        可以返回添加课程成功后的课程id，方便后续添加课程大纲等内容
        String courseId = eduCourseService.addCourseInfo(courseInfoVO);
        return ResultEntity.ok().data("courseId", courseId);
    }

    /**
     * 根据课程ID查询添加至数据库中的课程基本信息
     *
     * @param courseId 课程ID
     * @return result of operation
     */
    @GetMapping("getCourseInfo/{courseId}")
    public ResultEntity getCourseInfo(@PathVariable String courseId) {
        CourseInfoVO courseInfoVO = eduCourseService.getCourseInfo(courseId);
        return ResultEntity.ok().data("courseInfoVO", courseInfoVO);
    }

    /**
     * 修改课程基本信息
     *
     * @param courseInfoVO 课程基本信息
     * @return result of operation
     */
    @PostMapping("updateCourseInfo")
    public ResultEntity updateCourseInfo(@RequestBody CourseInfoVO courseInfoVO) {
        eduCourseService.updateCourseInfo(courseInfoVO);
        return ResultEntity.ok();
    }

    /**
     * 根据课程id查询课程确认信息
     *
     * @param courseId 课程id
     * @return result of operation
     */
    @GetMapping("getPublishCourseInfo/{courseId}")
    public ResultEntity getPublishCourseInfo(@PathVariable String courseId) {
        CoursePublishVo coursePublishVo = eduCourseService.getPublishCourseInfo(courseId);
        return ResultEntity.ok().data("info", coursePublishVo);
    }

    /**
     * 课程最终发布，修改课程状态
     *
     * @param courseId 课程id
     * @return result of operation
     */
    @PostMapping("publishCourse/{courseId}")
    public ResultEntity publishCourse(@PathVariable String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return ResultEntity.ok();
    }

    /**
     * 删除课程信息(包括课程基本信息，课程大纲，课程视频等相关信息)
     *
     * @param courseId 课程id
     * @return result of operation
     */
    @DeleteMapping("deleteCourseInfo/{courseId}")
    public ResultEntity deleteCourseInfo(@PathVariable String courseId) {
        eduCourseService.removeAllCourseInfo(courseId);
        return ResultEntity.ok();
    }
}

